package day06;

/**
 * 单例设计模式：定义单例时建议使用饿汉式
 * 
 * @author wangbiao
 * 
 */
public class Singleton {
	/*
	 * 饿汉式 private static final Singleton single = new Singleton();
	 * 
	 * private Singleton() { };
	 * 
	 * public static Singleton getInstance() { return single; }
	 */
	/**
	 * 懒汉式
	 */
	/*
	private static Singleton single = null;

	private Singleton() {

	}

	public static Singleton getInstance() {
		if (single == null) {
			single = new Singleton();
		}
		return single;
	}
	*/
	/**
	 * 双重判断延迟加载懒汉式，解决多线程访问的问题
	 */
	private static Singleton single = null;
	
	private Singleton() {
		
	}
	
	/**
	 * 减少了同步判断的次数
	 * @return
	 */
	public static Singleton getInstance() {
		if (single == null) {
			synchronized(Singleton.class) {
				if (single == null) {
					single = new Singleton();
				}
			}
		}
		return single;
	}
}
